var product = function(){

    //swiper配置
    var swiper = function () {
        var mySwiper = new Swiper('.swiper-container', {
            autoplay: 3000,
            pagination : '.swiper-pagination'
    })
    };

    //弹出购物车选择
    var cart = function(){
        pop('.callAddToCart','.directBuy','.addToCart');
    };
    //弹出立即购买
    var direct = function(){

        pop('.callDirectBuy','.addToCart','.directBuy');
    };
    function pop(button,hideBtn,showBtn){
    $(button).on('click',function(){
        $('.mask').css('display','block');
        $('.pop-box').slideDown();
        $('.buyNum').val(1);
        $(hideBtn).css('display','none');
        $(showBtn).css('display','block');
    });
    $('.mask').on('click',function(){
        $('.pop-box').slideUp();
        $('.mask').css('display','none');
    })
}

//加减数量
    var count = function () {
        $('.cut-icon').on('click',function(){
            var num = Number($('.buyNum').val());
            if(num<=1){
                messageBox("不能再少了");
            }else{
                num=num-1;
            }
            $('.buyNum').val(num);
        });
        $('.plus-icon').on('click',function(){
            var num = Number($('.buyNum').val());
            num=num+1;
            $('.buyNum').val(num);
        });
    };


    //添加到购物车
    var addToCart = function(){
        $('.addToCart').on('click',function(){
            if (getUserLoginStatus()) {
                //获得传递参数
                var buyNum = $('.buyNum').val();
                var productId = $(this).attr('data-productId');
                if (!productId) {
                    messageBox("获取商品ID失败");
                    return;
                }
                //请求接口,获取结果
                apiRequest('POST', '/cart', {product: productId, productCount: buyNum}).then(function (results) {
                    if (results.code == 200) {
                        console.log(results);
                        messageBox('添加成功啦，快去购物车查看吧');
                        $('.pop-box').slideUp();
                        $('.mask').css('display','none');
                    }else if(results.code == 103){
                        doLogin();
                    }else {
                        messageBox(results.message);
                    }
                }, function (err, error) {
                    console.log(err, error);
                    messageBox("网络错误");
                })
            }else{
                doLogin();
            }
        });
    };

    //立即购买
    var directBuy = function(){
        $('.directBuy').on('click',function(){
            if (getUserLoginStatus()) {
                //获得传递参数
                var count = $('.buyNum').val();
                var productId = $(this).attr('data-productId');
                if (!count) {
                    count = 1;
                }
                if (!productId) {
                    messageBox("无法获得商品信息");
                    return;
                }
                var checkProduct = [];
                var temp = {};
                temp.product = productId;
                temp.count = count;
                checkProduct.push(temp);
                var products = {
                    products: checkProduct
                };
                var params = new Base64().encode(JSON.stringify(products));
                window.location.href = '/confirm?params=' + params;
            }else{
                doLogin();
            }
        });
    };

    //点击进入购物车
    var goCart = function () {
        $('.goCart').on('click',function (e) {
            e.preventDefault();
            if (getUserLoginStatus()) {
                window.location.href="/cart";
            } else {
                doLogin();
            }
        })
    };

    //点击进入订单列表
    var goOrderList = function(){
        $('.goOrderList').on('click',function (e) {
            e.preventDefault();
            if (getUserLoginStatus()) {
                window.location.href="/orders";
            } else {
                doLogin();
            }
        })
    };
    return {
        init:function(){
            swiper();
            cart();
            direct();
            count();
            addToCart();
            directBuy();
            goCart();
            goOrderList();
        }
    }
}();
